capture program drop myround
program define myround, eclass
	syntax varlist (min=1 max=1 numeric), decimals(numlist min=1 max=1)
	
	tokenize `varlist'
	local var "`1'"
	
	tostring `var', gen(temp_str) force

	replace temp_str="0.00000" if strpos(temp_str,"e-")>0 & substr(temp_str,1,1)!="-"
	replace temp_str="-0.00000" if strpos(temp_str,"e-")>0 & substr(temp_str,1,1)=="-"
	
	split temp_str, p(".")
	local nv=r(nvars)
	if `nv'>1 {
		replace temp_str1="0" if temp_str1==""
		replace temp_str1="-0" if temp_str1=="-"
		
		local flag=0
		while `flag'==0 {
			cap drop length2
			gen length2=length(temp_str2)
			replace temp_str2=temp_str2+"0" if length2<`decimals'+1
			
			drop length2
			gen length2=length(temp_str2)
			quietly sum length2
			local min=r(min)-1
			if `min'>=`decimals' {
				local flag=1
			}
		}
		cap drop length2
		
		gen var_first_tpositions=substr(temp_str2,1,`decimals')
		local t=`decimals'+1
		gen var_tthpos=substr(temp_str2,`t',1)
		destring var_first_tpositions var_tthpos, force replace
		replace var_first_tpositions=var_first_tpositions+1 if var_tthpos>=5
		replace var_first_tpositions=var_first_tpositions+0 if var_tthpos<5
		
		tostring var_first_tpositions, force replace
		local flag=0
		while `flag'==0 {
			cap drop length1
			gen length1=length(var_first_tpositions)
			replace var_first_tpositions="0"+var_first_tpositions if length1<`decimals'
			
			cap drop length1
			gen length1=length(var_first_tpositions)
			quietly sum length1
			local min=r(min)
			if `min'>=`decimals' {
				local flag=1
			}
		}
		cap drop length1
		
		rename `var' vv
		gen `var'=temp_str1+"."+var_first_tpositions
		drop temp_str* var_first_tpositions var_tthpos
		replace `var'="" if vv==.
		drop vv
	}
	else {
		tostring `var', force replace
		drop temp*
	}
end
